<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分队管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-form :inline="true" class="searchForm">
      <el-form-item>
        <el-input v-model="keyword" clearable placeholder="请输入要搜索的关键字" @clear="clear"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
      </el-form-item>

      <el-form-item>
        <el-select placeholder="选择年级" clearable filterable v-model="gradeId" @change="selectChange">
          <el-option v-for="item in gradeList" :key="item.id" :label="item.gradeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select placeholder="选择职务" clearable filterable v-model="roleId" @change="selectChange">
          <el-option label="骨干" :value="0"></el-option>
          <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select v-model="status" placeholder="选择状态" clearable @change="selectChange">
          <el-option label="预备队员" :value="0"></el-option>
          <el-option label="正式队员" :value="1"></el-option>
          <el-option label="退队队员" :value="-1"></el-option>
          <el-option label="退役队员" :value="-2"></el-option>
        </el-select>
      </el-form-item>
      <div v-if="status!==''&&status!==null">
        <el-form-item>
          <el-button type="primary" @click="regular" plain icon="el-icon-s-tools" v-show="status == 0">转正</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="danger" @click="leave" plain icon="el-icon-s-tools" v-show="status >=0">退队</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" @click="retire" plain icon="el-icon-s-tools" v-show="status == 1">退役</el-button>
        </el-form-item>
      </div>
    </el-form>
    <!-- 表单 -->

    <el-table :data="dataList" border stripe @selection-change="selectionChange" v-loading="loading" height="550px">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="姓名" prop="realName" min-width="80px" align="center"></el-table-column>
      <el-table-column label="性别" min-width="50px" align="center">
        <template slot-scope="scope">
          {{ scope.row.sex == 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column label="qq账号" prop="qq" min-width="100px" align="center"></el-table-column>
      <el-table-column label="状态" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0" type="info" size="normal" effect="dark">预备队员</el-tag>
          <el-tag v-else-if="scope.row.status==1" type="success" size="normal" effect="dark">正式队员</el-tag>
          <el-tag v-else-if="scope.row.status==-1" type="danger" size="normal" effect="dark">退队队员</el-tag>
          <el-tag v-else-if="scope.row.status==-2" type="warning" size="normal" effect="dark">退役队员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="班级" prop="classes.className" min-width="100px" align="center"></el-table-column>
      <el-table-column label="职务" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-popover placement="top" trigger="hover">
            <el-tag type="success" size="normal" effect="plain">{{
              scope.row.unit ? scope.row.unit.unitName : ""
            }}</el-tag>
            <span slot="reference" class="roleName">{{
              scope.row.role ? scope.row.role.roleName : ""
            }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="150px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="detail(scope.row)" icon="el-icon-s-tools">详情</el-button>
          <el-button size="mini" type="primary" @click="edit(scope.row)" icon="el-icon-s-tools">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page.page" :total="page.total" background layout="prev, pager, next" @current-change="currentChange">
    </el-pagination>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑" :visible.sync="dialogEdit" center v-loading="loading">
      <el-form class="form" :model="form" label-left label-width="100px" :rules="rules" ref="editForm">
        <el-form-item label="状态" size="normal">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="预备队员" :value="0"></el-option>
            <el-option label="正式队员" :value="1"></el-option>
            <el-option label="退队队员" :value="-1"></el-option>
            <el-option label="退役队员" :value="-2"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doEdit('editForm')">确 定</el-button>
        <el-button @click="dialogEdit = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框 -->
    <el-dialog title="新增" :visible.sync="dialogAdd" center v-loading="loading">
      <el-form class="form" :model="form" label-left label-width="100px" :rules="rules" ref="addForm">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model.number="form.phone"></el-input>
        </el-form-item>

        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model.trim="form.realName"></el-input>
        </el-form-item>

        <el-form-item label="昵称" prop="nickName">
          <el-input v-model.trim="form.nickName"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>

        <el-form-item label="学号" prop="unique">
          <el-input v-model.number="form.unique"></el-input>
        </el-form-item>

        <el-form-item label="qq号码" prop="qq">
          <el-input v-model.number="form.qq"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doAdd('addForm')">确 定</el-button>
        <el-button @click="dialogAdd = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  adminUserPageApi as getPageApi,
  adminEditUserStatusApi as editApi,
  adminGetGradeListApi as getGradeList,
  adminGetRoleListApi as getRoleList,
  adminEditUserRegularApi as regular,
  adminEditUserLeaveApi as leave,
  adminEditUserRetireApi as retire,
} from "@/api/admin.js";
export default {
  props: ["id"],
  inject: ["reload"],
  data() {
    return {
      //表格展示数据
      dataList: [],
      //分页数据
      page: {
        page: 1,
        limit: 10,
        total: 0,
      },
      //选择的数据
      select: [],
      //加载
      loading: false,
      //表单数据
      form: {}, //表单数据
      keyword: "", //关键字
      dialogEdit: false, //显示编辑对话款
      dialogAdd: false, //显示编辑对话款
      //表单校验
      rules: {
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
        ],
        realName: {
          required: true,
          message: "请输入真实姓名",
          trigger: "blur",
        },
        nickName: {
          required: true,
          message: "请输入昵称",
          trigger: "blur",
        },
        sex: {
          required: true,
          message: "请选择性别",
          trigger: "blur",
        },
        unique: [
          {
            required: true,
            message: "请输入学号",
            trigger: "blur",
          },
        ],
        qq: [
          {
            required: true,
            message: "请输入qq号码",
            trigger: "blur",
          },
        ],
        gradeId: {
          required: true,
          message: "请选择年级",
          trigger: ["blur", "change"],
        },
        classId: {
          required: true,
          message: "请选择班级",
          trigger: ["blur", "change"],
        },
        unitId: {
          required: true,
          message: "请选择分队",
          trigger: ["blur", "change"],
        },
      },
      gradeList: [],
      roleList: [],
      roleId: null,
      gradeId: null,
      status: null,
    };
  },
  methods: {
    //获取数据列表
    getList(
      page = this.page.page,
      limit = this.page.limit,
      keyword = this.keyword,
      gradeId = this.gradeId,
      roleId = this.roleId,
      unitId = this.id,
      status = this.status
    ) {
      let queryParam = {
        page,
        limit,
        keyword,
        gradeId,
        roleId,
        unitId,
        status,
      };
      getPageApi(queryParam)
        .then((res) => {
          this.loading = true;
          const result = res.data;
          if (result.status === 200) {
            this.dataList = result.data.content;
            this.page.total = result.data.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //选择
    selectionChange(e) {
      this.select = e;
    },
    //分页
    currentChange(e) {
      this.page.page = e;
      this.getList();
    },
    //打开编辑对话款
    edit(e) {
      this.form = e;
      this.dialogEdit = true;
    },
    //执行编辑操作
    doEdit(e) {
      this.$refs[e].validate((valid) => {
        if (valid) {
          editApi(this.form)
            .then((res) => {
              this.loading = true;
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("修改成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.loading = false;
              this.dialogEdit = false;
              this.reload();
            });
        }
      });
    },
    //搜索
    search() {
      this.getList();
    },
    //清空文本框
    clear() {
      this.getList();
    },

    //获取年级列表
    getGradeList() {
      getGradeList().then((res) => {
        const result = res.data;
        if (result.status == 200) {
          this.gradeList = result.data;
        }
      });
    },
    //获取职务列表
    getRoleList() {
      getRoleList().then((res) => {
        const result = res.data;
        if (result.status == 200) {
          this.roleList = result.data;
        }
      });
    },
    //转正
    regular() {
      this.$confirm("确认要转正吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        const ids = this.select.map((data) => {
          return data.id;
        });
        regular({ ids })
          .then((res) => {
            this.loading = true;
            const result = res.data;
            if (result.status == 200) {
              this.$message.success("转正成功");
            } else {
              this.$message.error(result.msg);
            }
          })
          .finally(() => {
            this.loading = false;
            this.dialogEdit = false;
            this.reload();
          });
      });
    },
    //退队
    leave() {
      this.$confirm("确认要让该用户退队吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        const ids = this.select.map((data) => {
          return data.id;
        });
        leave({ ids })
          .then((res) => {
            this.loading = true;
            const result = res.data;
            if (result.status == 200) {
              this.$message.success("退队成功");
            } else {
              this.$message.error(result.msg);
            }
          })
          .finally(() => {
            this.loading = false;
            this.dialogEdit = false;
            this.reload();
          });
      });
    },
    //退役
    retire() {
      this.$confirm("确认要让该用户退役吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        const ids = this.select.map((data) => {
          return data.id;
        });
        retire({ ids })
          .then((res) => {
            this.loading = true;
            const result = res.data;
            if (result.status == 200) {
              this.$message.success("退役成功");
            } else {
              this.$message.error(result.msg);
            }
          })
          .finally(() => {
            this.loading = false;
            this.dialogEdit = false;
            this.reload();
          });
      });
    },

    //点击选择筛选条件
    selectChange(e) {
      this.getList();
    },

    detail(e) {
      this.$router.push("/admin/home/user/detail/" + e.id);
    },
  },
  created() {
    this.getList();
    this.getGradeList();
    this.getRoleList();
  },
};
</script>

<style scoped>
.el-tabs {
  margin-top: 50px;
}
.el-pagination {
  float: right;
  margin: 20px;
}
.searchForm {
  margin-left: 100px;
  margin-top: 20px;
}
.form {
  padding: 20px 50px;
}
.form .el-form-item {
  width: 300px;
  margin: 20px auto;
}
.roleName:hover {
  cursor: pointer;
}
</style>
